package com.hh.demo.mq;

import com.alibaba.fastjson.JSON;
import com.hh.demo.domain.MediaFile;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 黄浩
 * @create 2022-07-25 17:59
 */
@Component
public class MediaFileProducer {
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 发送同步消息，返回是否发送成功
     * @param mediaFile
     * @return
     */
    public boolean sendMessage(MediaFile mediaFile){
        String mediaFileJsonStr = JSON.toJSONString(mediaFile);
        SendResult sendResult = rocketMQTemplate.syncSend("media-topic:media-tags", mediaFileJsonStr);
        return sendResult.getSendStatus()== SendStatus.SEND_OK;
    }
}
